package com.study.spmongodb.controller;

import com.study.spmongodb.article.po.Comment;
import com.study.spmongodb.article.service.CommentService;
import com.study.spmongodb.config.WebConfigImpl;
import com.study.spmongodb.config.webConfig;
import com.study.spmongodb.im.pojo.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author MI
 * @version 1.0
 * @date 2020/10/20 11:37
 */
@RestController
@RequestMapping("hello")
public class HelloController {

    private static final Lock lock = new ReentrantLock();
    @Autowired
    CommentService commentService;
    @Autowired
    ThreadService threadService;
    int a = 0;

    @GetMapping("comment")
    public Object comment() {
        final Page<Comment> commentPage = commentService.findAllPage(1, 2);
        System.out.println(commentPage);
        final List<Comment> contentList = commentPage.getContent();
        System.out.println(contentList);
        return commentPage;
    }

    @GetMapping("date")
    public Date date() {
        // 返回 IOC 容器，使用注解配置，传入配置类
        ApplicationContext context = new AnnotationConfigApplicationContext(WebConfigImpl.class);
        User user = context.getBean(User.class);
        System.out.println(user);
        return new Date();
    }

    @GetMapping("beanName")
    public String beanName() {
        ApplicationContext context = new AnnotationConfigApplicationContext(WebConfigImpl.class);
        // 查看 User 这个类在 Spring 容器中叫啥玩意
        String[] beanNames = context.getBeanNamesForType(User.class);
        Arrays.stream(beanNames).forEach(System.out::println);


        final String[] beanDefinitionNames = context.getBeanDefinitionNames();
        Arrays.stream(beanDefinitionNames).forEach(System.out::println);

        final Map<String, webConfig> beansOfType = context.getBeansOfType(webConfig.class);

        final String[] webConfig = context.getBeanNamesForType(webConfig.class);
        System.out.println("接口 beanName" + Arrays.toString(webConfig));


        return beanNames.toString();
    }

    @GetMapping("thread")
    public String threadName() {

        // lock.lock();
        try {
            threadService.threadName();
            System.out.println(Thread.currentThread().getName());
            System.out.println(StringUtils.repeat("--", "*", 30));
            a = a + 1;
            System.out.println(a);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // lock.unlock();
        }
        return Thread.currentThread().getName();

    }

}

